﻿using System.Collections.Generic;
using System.Linq;
using Super_Management_Mayhem.Content.Spritesheet;
using Transformable_Engine_v2.Engine;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.Screens.Result
{
    public class ResultScreen : Container
    {
        private const float TOP_CURTAIN_OFFSET = 3.75f;

        public ResultScreen(List<TeamResult> results)
        {
            Container curtainContainer = new Container(2, 2);

            for (int i = 0; i < (Game.Options.ScreenSize.X - 4) / TOP_CURTAIN_OFFSET; i++)
            {
                curtainContainer.AddChildren(new ImageContainer {
                                                                    PositionGridBound = new TE_Vector2(TOP_CURTAIN_OFFSET * i, 0),
                                                                    Sprite = ResultSheet.GetSprite(ResultSheet.Curtains.Top),
                                                                    Origin = TE_Rectangle.Alignment.TopMiddle
                                                                });
            }

            curtainContainer.AddChildren(new ImageContainer {
                                               PositionGridBound = new TE_Vector2(0, 0),
                                               Sprite = ResultSheet.GetSprite(ResultSheet.Curtains.TopLeftCorner),
                                               Origin = TE_Rectangle.Alignment.TopLeft
                                           });
            curtainContainer.AddChildren(new ImageContainer {
                                               PositionGridBound = new TE_Vector2(Game.Options.ScreenSize.X - 4, 0),
                                               Sprite = ResultSheet.GetSprite(ResultSheet.Curtains.TopRightCorner),
                                               Origin = TE_Rectangle.Alignment.TopRight
                                           });

            var title = new ImageContainer {
                                               PositionGridBound = new TE_Vector2(Game.Options.ScreenSize.X / 2, 4.5f),
                                               Sprite = ResultSheet.GetSprite(ResultSheet.Other.Title),
                                               Origin = TE_Rectangle.Alignment.TopMiddle
                                           };

            Container barContainer = new Container(Game.Options.ScreenSize.X / 2, (28 + Game.Options.ScreenSize.Y - 4) / 2);

            float maxScore = results.Max(r => r.TotalScore);
            for (int i = 0; i < results.Count; i++)
            {
                barContainer.AddChildren(new Bar(results[i], results[i].TotalScore / maxScore) { PositionGridBound = new TE_Vector2((i - (results.Count - 1) / 2.0f) * 17, 0)});
            }

            AddChildren(title, curtainContainer, barContainer);
        }
    }
}